package com.mxt.moviedemo.controller;

import com.mxt.moviedemo.entity.Geners;
import com.mxt.moviedemo.service.MoviesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("geners")
public class GenersController {
    private static final Logger logger = LoggerFactory.getLogger(GenersController.class);
    @Autowired
    private MoviesService moviesService;
    @GetMapping("/searchGeners")
    public Map<String, Object> searchGeners(@RequestParam("gname") String gname) {
        logger.info("获取类别信息，id：{}", gname);
        Map<String, Object> result = new HashMap<>();

        try {
            List<Geners> geners=moviesService.searchGenersByName(gname);
            if (geners != null) {
                result.put("success", true);
                result.put("geners", geners);
            } else {
                result.put("success", false);
                result.put("error", "未找到该类别");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询类别信息失败：" + e.getMessage());
        }
        return result;
    }
    @GetMapping("/infoGeners")
    public Map<String, Object> infoGeners(@RequestParam("gid") int gid) {
        logger.info("获取类别信息，id：{}", gid);
        Map<String, Object> result = new HashMap<>();

        try {
            Geners geners = moviesService.getGenersById(gid);
            if (geners != null) {
                result.put("success", true);
                result.put("geners", geners);
            } else {
                result.put("success", false);
                result.put("error", "未找到该类别");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询类别信息失败：" + e.getMessage());
        }
        return result;
    }

    // 新增订票
    @PostMapping("addGeners")
    public Map<String, Object> addGeners(@RequestBody Geners geners) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = moviesService.addGeners(geners);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "类别信息添加成功");
            } else {
                result.put("success", false);
                result.put("message", "类别信息添加失败");
            }
        } catch (Exception e) {
            logger.error("添加类别信息错误: ", e);
            result.put("success", false);
            result.put("message", "类别信息添加失败：" + e.getMessage());
        }
        return result;
    }

    @PutMapping("updateGeners")
    public Map<String, Object> updateGeners(@RequestBody Geners geners) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (moviesService.updateGeners(geners)) {
                result.put("success", true);
                result.put("message", "类别信息修改成功");
            } else {
                result.put("success", false);
                result.put("message", "类别信息修改失败");
            }
        } catch (Exception e) {
            logger.error("类别信息错误: ", e);
            result.put("success", false);
            result.put("message", "类别信息修改失败：" + e.getMessage());
        }
        return result;
    }
    @DeleteMapping("deleteGeners/{gid}")
    public Map<String, Object> deleteGeners(@PathVariable int gid) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = moviesService.deleteGeners(gid);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "类别信息删除成功");
            } else {
                result.put("success", false);
                result.put("message", "类别信息删除失败");
            }
        } catch (Exception e) {
            logger.error("删除类别信息错误: ", e);
            result.put("success", false);
            result.put("message", "类别信息删除失败：" + e.getMessage());
        }
        return result;
    }
}
